﻿using C1.WPF;
using C1.WPF.FlexGrid;
using Model.Modules;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace SteelDetection
{
    /// <summary>
    /// PageHistory.xaml 的交互逻辑
    /// </summary>
    public partial class PageHistoryAlerm : Page
    {
        public PageHistoryAlerm()
        {
            InitializeComponent();
            Loaded += PageHistory_Loaded;
            this.SetBinding(CurrentListProperty, "CurrentList");
        }

        List<ScrollViewer> scrollViewers = new List<ScrollViewer>();
        private void PageHistory_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentList.CollectionChanged += CurrentList_CollectionChanged;


            steel_defect_grid.ColumnHeaders.Rows[0].Height = 60;
        }

        private void CurrentList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

        }

        private void PageHistory_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            //禁用以提升性能
            //currentimgindexCtl.Width = (int)e.VerticalOffset;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            scrollViewers[0].ScrollToVerticalOffset(0);
        }

        private void defectlistviewer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void imgaelistview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //scrollViewers[0].ScrollToVerticalOffset(imgaelistview.SelectedIndex);
            //scrollViewers[1].ScrollToVerticalOffset(imgaelistview.SelectedIndex);
        }


        public ObservableCollection<Imagedefectinfo> CurrentList
        {
            get
            {
                return (ObservableCollection<Imagedefectinfo>)GetValue(CurrentListProperty);
            }
            set
            {
                SetValue(CurrentListProperty, value);

            }
        }
        public static readonly DependencyProperty CurrentListProperty = DependencyProperty.RegisterAttached("CurrentList", typeof(ObservableCollection<Imagedefectinfo>), typeof(PageHistoryAlerm), new FrameworkPropertyMetadata(null, new PropertyChangedCallback((s, e) => {
            if (e.NewValue != null)
            {
                ObservableCollection<Imagedefectinfo> imagedefectinfos = e.NewValue as ObservableCollection<Imagedefectinfo>;
                foreach(var info in imagedefectinfos)
                {
                   
                }

            }

        })));

        private void C1ComboBox_SelectedItemChanged(object sender, C1.WPF.PropertyChangedEventArgs<object> e)
        {

        }

        private void C1FlexGrid_SelectedItemChanged(object sender, EventArgs e)
        {
            //var item = (sender as C1.WPF.FlexGrid.C1FlexGrid).SelectedItem;
            //CurrentImg = (Imagedefectinfo)item;
        }

        private void C1ComboBox_SelectedIndexChanged(object sender, PropertyChangedEventArgs<int> e)
        {
            var selectitem = (sender as C1ComboBox).SelectedItem;
            if (selectitem == null) return;
            string filter = (selectitem as C1ComboBoxItem).Content.ToString();
            switch (filter)
            {
                case "全部":
                    steel_defect_grid.CollectionView.Filter = null;
                    break;
                case "警告":
                    steel_defect_grid.CollectionView.Filter = IsWarning;
                    break;
                case "报警":
                    steel_defect_grid.CollectionView.Filter = IsAlert;
                    break;

            }
        }

        private bool IsWarning(object obj)
        {
            if (obj as steeldefectinfo != null
               && (obj as steeldefectinfo).Steel_defect.grade == "警告")
            {
                return true;
            }
            return false;
        }
        private bool IsAlert(object obj)
        {
            if (obj as steeldefectinfo != null
               && (obj as steeldefectinfo).Steel_defect.grade == "报警")
            {
                return true;
            }
            return false;
        }

        private void grid_ItemsSourceChanged(object sender, EventArgs e)
        {
            var grid = (sender as C1FlexGrid);
            if(grid.Rows.Count > 0){
                grid.SelectedIndex = -1;
                grid.SelectedIndex = 0;
            }
        }
    }





}
